package xin.printk.android.view.adapter;

import android.content.Context;
import android.support.constraint.ConstraintLayout;
import android.support.v7.widget.RecyclerView;
import android.support.v7.widget.helper.ItemTouchHelper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import java.util.List;

import xin.printk.android.R;
import xin.printk.android.data.entity.NoteItem;
import xin.printk.android.data.entity.Theme;

public class ThemeGridAdapter extends AbstractRecyclerAdapter<Theme> {

    public ThemeGridAdapter(Context context, List<Theme> list) {
        super(context, list);
    }

    @Override
    public RecyclerView.ViewHolder createContentHolder(ViewGroup parent) {
        return new GridHolder(LayoutInflater.from(mContext)
                .inflate(R.layout.item_theme, parent, false));
    }

    @Override
    public void bindContentHolder(RecyclerView.ViewHolder holder, int position) {
        GridHolder gridHolder = (GridHolder) holder;
        gridHolder.layout.setBackgroundColor(mData.get(position).getColor());
        gridHolder.description.setText(mData.get(position).getDescription());
    }

    static class GridHolder extends RecyclerView.ViewHolder {

        ConstraintLayout layout;
        TextView description;

        GridHolder(View itemView) {
            super(itemView);
            layout = itemView.findViewById(R.id.item_layout);
            description = itemView.findViewById(R.id.tv_theme_description);
        }
    }

    public static class ItemDragCallback extends ItemTouchHelper.Callback {

        ThemeGridAdapter adapter;

        public ItemDragCallback(ThemeGridAdapter adapter) {
            this.adapter = adapter;
        }

        @Override
        public int getMovementFlags(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder) {
            int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.LEFT | ItemTouchHelper.RIGHT;
            int swipeFlags = 0;
            return makeMovementFlags(dragFlags, swipeFlags);
        }

        @Override
        public boolean onMove(RecyclerView recyclerView, RecyclerView.ViewHolder viewHolder, RecyclerView.ViewHolder target) {
            int fromPosition = viewHolder.getAdapterPosition();
            int toPosition = target.getAdapterPosition();
            adapter.notifyItemMoved(fromPosition, toPosition);
            return true;
        }

        @Override
        public void onSwiped(RecyclerView.ViewHolder viewHolder, int direction) {

        }

        @Override
        public boolean isLongPressDragEnabled() {
            return true;
        }
    }

}
